package com.atguigu.controller;


import com.atguigu.cache.ShopCache;
import com.atguigu.entity.BaseBrand;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseBrandService;
import com.atguigu.utils.MinioUploader;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.minio.MinioClient;
import io.minio.errors.MinioException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * <p>
 * 品牌表 前端控制器
 * </p>

 */
@RestController
@RequestMapping("/product/brand")
@Api(tags = "品牌管理接口")
public class BaseBrandController {
    @Autowired
    private MinioUploader minioUploader;

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private BaseBrandService baseBrandService;

    //http://127.0.0.1/product/brand/getAllBrand
    @ApiOperation("获取所有的品牌列表")
   // @ShopCache(prefix = "allBrand",enableBloom = false)
    @GetMapping("getAllBrand")
    public RetVal getAllBrand(){
        List<BaseBrand> list = baseBrandService.list(null);
        return RetVal.ok(list);
    }

    //http://127.0.0.1/product/brand/queryBrandByPage/1/10
    @ApiOperation("获取品牌列表并分页展示")
    @GetMapping("queryBrandByPage/{page}/{size}")
    public RetVal queryBrandByPage(@PathVariable Long page,
                                   @PathVariable Long size){
        // 创建一个Page 对象
        Page<BaseBrand> spuInfoPage = new Page<>(page,size);
        // 获取数据
        IPage<BaseBrand> spuInfoPageList = baseBrandService.page(spuInfoPage,null);
        // 将获取到的数据返回即可！
        return RetVal.ok(spuInfoPageList);
    }
    //http://127.0.0.1/product/brand/6
    @ApiOperation("根据平牌id获取单个品牌")
    @GetMapping("{baseBrandId}")
    public RetVal get(@PathVariable Long baseBrandId) {
        BaseBrand baseBrand = baseBrandService.getById(baseBrandId);
        System.out.println(baseBrandId);
        return RetVal.ok(baseBrand);
    }
    //http://127.0.0.1/product/brand
    @ApiOperation("保存品牌信息")
    @PostMapping
    public RetVal save(@RequestBody BaseBrand baseBrand) {
        baseBrandService.save(baseBrand);
        return RetVal.ok();
    }

    @PutMapping
    public RetVal updateById(@RequestBody BaseBrand baseBrand) {
        //删除 minos 图片
        //String logoUrl = baseBrand.getBrandLogoUrl();
        //try {
        //    // 从mybucket中删除myobject。
        //    minioClient.removeObject("gmall",logoUrl.substring(logoUrl.lastIndexOf("/")+1));
        //    System.out.println("successfully removed mybucket/myobject");
        //} catch (Exception e) {
        //    System.out.println("Error: " + e);
        //}
        baseBrandService.updateById(baseBrand);

        return RetVal.ok();
    }

    //5.删除品牌信息
   // http://127.0.0.1/product/brand/6
    @DeleteMapping("{brandId}")
    @ApiOperation("删除品牌信息")
    public RetVal remove(@PathVariable Long brandId) {

        //BaseBrand b = baseBrandService.getById(brandId);
        //String logoUrl = b.getBrandLogoUrl();
        //try {
        //    // 从mybucket中删除myobject。
        //    minioClient.removeObject("gmall",logoUrl.substring(logoUrl.lastIndexOf("/")+1));
        //    System.out.println("successfully removed mybucket/myobject");
        //} catch (Exception e) {
        //    System.out.println("Error: " + e);
        //}
        baseBrandService.removeById(brandId);
        return RetVal.ok();
    }


    //7.上传文件的接口
    //http://api.gmall.com/product/brand/fileUpload
    @PostMapping("fileUpload")
    @ApiOperation("上传文件")
    public RetVal fileUpload(MultipartFile file) throws Exception {
        String retUrl = minioUploader.uploader(file);
        return RetVal.ok(retUrl);
    }


    //8.根据id查询品牌信息
    @GetMapping("getBrandById/{brandId}")
    public BaseBrand getBrandById(@PathVariable Long brandId) {
        BaseBrand brand = baseBrandService.getById(brandId);
        return brand;
    }

}
